﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using YoutubeVideo;
using YoutubeVideo.MyClass;

namespace YoutubeVideo
{
    class StorageHelper
    {
        public void SaveToXml(string fileName, List<VideoItem> classInstanceToSave)
        {
            using (IsolatedStorageFile isolatedStorage = GetIsolatedStorageFile)
            {
                using (IsolatedStorageFileStream stream = isolatedStorage.OpenFile(fileName, FileMode.Create))
                {
                    XmlSerializer serializer = new XmlSerializer(classInstanceToSave.GetType());

                    if(serializer!=null)

                    using (XmlWriter xmlWriter = XmlWriter.Create(stream, new XmlWriterSettings() { Indent = true }))
                    {
                        serializer.Serialize(xmlWriter, classInstanceToSave);
                    }
                }
            }
        }

        /// <summary>
        /// Gets the Isolated Storage File for the current platform.
        /// </summary>
        private IsolatedStorageFile GetIsolatedStorageFile
        {
            get
            {
#if (WINDOWS_PHONE)
                return IsolatedStorageFile.GetUserStoreForApplication();
#else
        return IsolatedStorageFile.GetUserStoreForDomain();
#endif
            }
        }
        public List<VideoItem> LoadFromXml(string fileName)
        {
            try
            {
                using (IsolatedStorageFile isolatedStorage = GetIsolatedStorageFile)
                {
                    // If the file exists, try and load it it's data.
                    if (isolatedStorage.FileExists(fileName))
                    {
                        using (IsolatedStorageFileStream stream = isolatedStorage.OpenFile(fileName, FileMode.Open))
                        {
                            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(List<VideoItem>));
                            List<VideoItem> data = (List<VideoItem>)serializer.Deserialize(stream);
                            return data;
                        }
                    }
                }
            }
            // Eat any exceptions unless debugging so that users don't see any errors.
            catch
            {

            }

            // We couldn't load the data, so just return a default instance of the class.
            return default(List<VideoItem>);
        }

        /// <summary>
        /// Gets if we are debugging the application or not.
        /// </summary>
        private bool IsDebugging
        {
            get
            {
#if (DEBUG)
                // Extra layer of protection in case we accidentally release a version compiled in Debug mode.
                if (System.Diagnostics.Debugger.IsAttached)
                    return true;
#endif
                return false;
            }
        }
        public static void FixErrorXmlFile(string fileName)
        {
            using (IsolatedStorageFile File = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (File.FileExists(fileName))
                {
                    File.DeleteFile(fileName);
                }
            }
        }
    }
}
